package com.langchain4j.core.flow.result;

import lombok.Data;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 节点执行结果
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class NodeResult {
    
    /**
     * 节点ID
     */
    private String nodeId;
    
    /**
     * 执行状态
     */
    private ExecutionStatus status;
    
    /**
     * 输出数据
     */
    private Map<String, Object> outputs;
    
    /**
     * 错误信息
     */
    private String errorMessage;
    
    /**
     * 异常对象
     */
    private Throwable error;
    
    /**
     * 执行开始时间
     */
    private LocalDateTime startTime;
    
    /**
     * 执行结束时间
     */
    private LocalDateTime endTime;
    
    /**
     * 执行耗时（毫秒）
     */
    private Long duration;
    
    /**
     * 元数据
     */
    private Map<String, Object> metadata;
    
    /**
     * 执行状态枚举
     */
    public enum ExecutionStatus {
        SUCCESS("成功"),
        FAILED("失败"),
        CANCELLED("取消"),
        TIMEOUT("超时");
        
        private final String description;
        
        ExecutionStatus(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    /**
     * 创建成功结果
     */
    public static NodeResult success(String nodeId, Map<String, Object> outputs) {
        LocalDateTime now = LocalDateTime.now();
        return NodeResult.builder()
                .nodeId(nodeId)
                .status(ExecutionStatus.SUCCESS)
                .outputs(outputs)
                .startTime(now)
                .endTime(now)
                .duration(0L)
                .build();
    }
    
    /**
     * 创建失败结果
     */
    public static NodeResult failure(String nodeId, String errorMessage, Throwable error) {
        LocalDateTime now = LocalDateTime.now();
        return NodeResult.builder()
                .nodeId(nodeId)
                .status(ExecutionStatus.FAILED)
                .errorMessage(errorMessage)
                .error(error)
                .startTime(now)
                .endTime(now)
                .duration(0L)
                .build();
    }
    
    /**
     * 创建超时结果
     */
    public static NodeResult timeout(String nodeId) {
        LocalDateTime now = LocalDateTime.now();
        return NodeResult.builder()
                .nodeId(nodeId)
                .status(ExecutionStatus.TIMEOUT)
                .errorMessage("执行超时")
                .startTime(now)
                .endTime(now)
                .duration(0L)
                .build();
    }
    
    /**
     * 是否成功
     */
    public boolean isSuccess() {
        return ExecutionStatus.SUCCESS.equals(status);
    }
    
    /**
     * 是否失败
     */
    public boolean isFailed() {
        return ExecutionStatus.FAILED.equals(status);
    }
    
    /**
     * 获取输出值
     */
    public Object getOutput(String key) {
        return outputs != null ? outputs.get(key) : null;
    }
    
    /**
     * 获取输出值（带默认值）
     */
    public Object getOutput(String key, Object defaultValue) {
        return outputs != null ? outputs.getOrDefault(key, defaultValue) : defaultValue;
    }
} 